package com.example.lcu_kcb.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.lcu_kcb.User;
import com.example.lcu_kcb.db.SearchRecordOpenHelper;


/**
 * 这里用 SQLLite 实现，不用 Litepal，复习下基础知识
 */
public class UserDao {
    private volatile static UserDao userDao;
    private SQLiteOpenHelper mHelper;


    public UserDao(Context applicationContext) {
        mHelper = new SearchRecordOpenHelper(applicationContext);
    }

    public static UserDao getInstance(Context context) {
        if (userDao == null) {
            synchronized (UserDao.class) {
                if (userDao == null) {
                    userDao = new UserDao(context.getApplicationContext());
                }
            }
        }
        return userDao;
    }

    /**
     * 插入记录
     */
    public void insert(User user) {
       // ContentValues contentValues = new ContentValues();
       // contentValues.put(Constants.SEARCH_RECORD_COLUMN_NAME, record);
        String sql = "insert into t_user(username, password) values(?,?)";
        mHelper.getWritableDatabase().execSQL(sql, new String[]{user.getUsername(),user.getPassword()});
    }

    /**
     * 删除指定记录
     */
    public void delete(String record) {
        //mHelper.getWritableDatabase().delete(Constants.SEARCH_RECORD_TABLE_NAME, Constants.SEARCH_RECORD_COLUMN_NAME + "=?", new String[]{record});
    }

    /**
     * 删除所有记录
     */
    public void deleteAll() {
        //mHelper.getWritableDatabase().delete(Constants.SEARCH_RECORD_TABLE_NAME, null, null);
    }

    /**
     * 查询
     * @param user
     * @return
     */
    public Cursor query(User user){
        SQLiteDatabase sqLiteDatabase= mHelper.getReadableDatabase();
        String sql="select * from t_user where username=?and password=?";
        return sqLiteDatabase.rawQuery(sql,new String[]{user.getUsername(),user.getPassword()});
    }
}
